package com.hi.hiparty.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import com.hi.hiparty.dao.query.UserQuery;
import com.hi.hiparty.entity.User;
import com.hi.hiparty.jdbc.SqlMapClientTemplateCache;
import com.hi.hiparty.util.EncryptTools;
import com.hi.hiparty.util.IDGenerator;

@Repository
public class UserDao {

	private static final Log logger = LogFactory.getLog(UserDao.class);

	protected SqlMapClientTemplate getSqlMapClientTemplate() throws Exception {
		return SqlMapClientTemplateCache.getSqlMapClientTemplate("hiparty");
	}

	public int checkLoginName(String loginname) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("loginname", loginname);
		return (Integer) this.getSqlMapClientTemplate().queryForObject("user.checkLoginName", params);
	}

	public Integer addUser(String loginname, String password) throws Exception {
		EncryptTools et = new EncryptTools();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("loginname", loginname);
		params.put("password", et.EncryptByMD5(password));
		params.put("time", new Date());
		return (Integer) this.getSqlMapClientTemplate().insert("user.addUser", params);
	}

	@SuppressWarnings("unchecked")
	public User checkLogin(String loginname, String password) throws Exception {
		EncryptTools et = new EncryptTools();
		Map<String, String> params = new HashMap<String, String>();
		params.put("loginname", loginname);
		params.put("password", et.EncryptByMD5(password));
		List<User> result = (List<User>) this.getSqlMapClientTemplate().queryForList("user.checkLogin", params);
		if (null == result || result.isEmpty()) {
			return null;
		}
		if (result.size() > 1) {
			logger.warn("出现重复的登录名：" + loginname);
			return result.get(0);
		}
		return result.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<User> getUserInfoPageForQuery(UserQuery q) throws Exception {
		return (List<User>) this.getSqlMapClientTemplate().queryForList("user.queryUsers", q);
	}

	public Integer getUserInfoCount(UserQuery q) throws Exception {
		return (Integer) this.getSqlMapClientTemplate().queryForObject("user.queryUsersCount", q);
	}

	public Integer updateUserNicknameAndGender(String userid, String nickname, String gender) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("userid", userid);
		params.put("nickname", nickname);
		params.put("gender", gender);
		return this.getSqlMapClientTemplate().update("user.updateUserNicknameAndGender", params);
	}

	public static void main(String[] args) throws Exception {
		UserDao userDao = new UserDao();
		// userDao.addUser("test", "test");
		System.out.println(userDao.checkLoginName("test"));
		System.out.println(userDao.checkLogin("test", "test"));
	}

	public User getUser(int userid) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);
		return (User) this.getSqlMapClientTemplate().queryForObject("user.getUser", params);
	}

	public Integer updateUserAvatar(String userid, String avatarUrl) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("userid", userid);
		params.put("avatarUrl", avatarUrl);
		return this.getSqlMapClientTemplate().update("user.updateUserAvatar", params);
	}

	public Integer updateUser(User user) throws Exception {
		return this.getSqlMapClientTemplate().update("user.updateUser", user);
	}
}
